Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Java programming Interviews Questions


Java Basis Question and Answer...

Q.1 What is an abstract class?

An abstract class in Java is a class that cannot be instantiated on its own and may contain abstract methods, which are methods without an implementation, meant to be subclassed and implemented.

Q.2 Describe synchronization in respect to multithreading.

Synchronization in multithreading ensures that only one thread can access a shared resource at a time, preventing data corruption and race conditions by using locks and monitors.

Q.3 What is the purpose of garbage collection in Java, and when is it used?

Garbage collection in Java automatically deallocates memory occupied by objects that are no longer referenced, preventing memory leaks and managing memory efficiently. It is used periodically by the JVM.

Q.4 What is the difference between a constructor and a method?

A constructor is a special type of method used for initializing objects, invoked when an object is created. Methods perform specific tasks and can be called multiple times after object initialization.

Q.5 What is the difference between an Interface and an Abstract class?

An interface defines a contract with abstract methods that implementing classes must fulfill, allowing multiple inheritance. An abstract class can have both abstract and concrete methods, providing partial implementation, but allows only single inheritance.

Q.6 Explain different way of using thread?

In Java, threads can be used in two main ways:

  1. Extending Thread Class: Create a subclass of Thread and override the run method.
  2. Implementing Runnable Interface: Implement the Runnable interface and pass an instance of the class to a Thread object, then call start.

Both methods involve defining the run method to specify the code that runs in the new thread.

Q.7 What is an Iterator?

An Iterator in Java is an interface that provides methods (hasNext(), next(), and remove()) to sequentially access and modify elements in a collection safely and efficiently.

Q.8 What is static in java?

In Java, static is a keyword used to indicate that a member (variable, method, block, or nested class) belongs to the class itself rather than to instances of the class. Static members can be accessed without creating an instance of the class.

Q.9 What is final class?

A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant).

Q.10 What if the main() method is declared as private?

The program compiles properly but at runtime it will give "main() method not public." message.

Q.11 What if the static modifier is removed from the signature of the main() method?

If the static modifier is removed from the signature of the main() method, the Java program will not run because the JVM needs a static main method as the entry point to start execution without requiring an instance of the class.

Q.12 What if I write static public void instead of public static void?

Program compiles and runs properly.

Q.13 What if I do not provide the String array as the argument to the method?

If you do not provide the String[] argument to the main method, the JVM will not recognize it as the entry point, leading to a runtime error with the message "Main method not found in class."

Q.14 What is the first argument of the String array in main() method?

The first argument of the String[] array in the main() method is the first command-line argument passed to the Java program. If no arguments are passed, the array is empty.


Q.15 How can one prove that the array is not null but empty using one line of code?

You can prove that the array is not null but empty using the following line of code in Java:


                System.out.println(array != null && array.length == 0);
              

Q.16 What environment variables do I need to set on my machine in order to be able to run Java programs?

To run Java programs, you need to set the JAVA_HOME variable to the JDK installation directory and add %JAVA_HOME%\bin to the PATH variable.

Q.17 Can an application have multiple classes having main() method?

Yes, an application can have multiple classes with a main() method. However, only one main() method acts as the entry point for the program, which is specified when running the application.

Q.18 Can I have multiple main() methods in the same class?

No, you cannot have multiple main() methods with the same signature in the same class. Only one main() method can serve as the entry point for the program.

Q.19 Do I need to import java.lang package any time? Why ?

No, you don't need to import the java.lang package explicitly. It is automatically imported by the compiler in every Java program because it contains fundamental classes and interfaces like String, Object, and System that are used by default

Q.20 What are Checked and UnChecked Exception?

Checked exceptions are exceptions that are checked at compile-time, meaning the compiler forces you to handle them using a try-catch block or declare them in the method signature using the throws keyword. Examples include IOException and SQLException. Unchecked exceptions are exceptions that are not checked at compile-time. They occur at runtime and are subclasses of RuntimeException. Examples include NullPointerException and ArrayIndexOutOfBoundsException.

Q.21 What is Overriding?

Overriding in Java refers to the process of providing a new implementation for a method inherited from a superclass in a subclass. The method signature in the subclass must match that of the superclass.

Q.22 What is the default value of an object reference declared as an instance variable?

The default value will be null unless we define it explicitly.

Q.23 Can a top level class be private or protected?

No. A top level class cannot be private or protected. It can have either "public" or no modifier. If it does not have a modifier it is supposed to have a default access. If a top level class is declared as private the compiler will complain that the "modifier private is not allowed here". This means that a top level class can not be private. Same is the case with protected.

Q.24 What type of parameter passing does Java support?

In Java the arguments are always passed by value.

Q.25 Primitive data types are passed by reference or pass by value?

In Java, primitive data types are passed by value. This means that when you pass a primitive type to a method, a copy of the value is passed, not the actual variable itself.

Q.26 Objects are passed by value or by reference?

In Java, objects are passed by value. When you pass an object to a method, you're passing the reference to the object, not the object itself.

Q.27 What is serialization?

Serialization in Java refers to the process of converting an object into a stream of bytes so that it can be stored in memory, a file, or sent over the network. This allows the object's state to be saved and later reconstructed.

Q.28 How do I serialize an object to a file?

To serialize an object to a file in Java, you can follow these steps:

  1. Create an instance of ObjectOutputStream by wrapping a FileOutputStream with the file path where you want to store the serialized object.
  2. Use the writeObject() method of ObjectOutputStream to write the object to the file.
  3. Close the ObjectOutputStream to release system resources.

Q.29 Which methods of Serializable interface should I implement?

In Java, the Serializable interface is a marker interface, meaning it does not have any methods to implement. Simply implementing it enables the serialization mechanism for the class.

Q.30 What is Externalizable interface?

The Externalizable interface in Java is an extension of the Serializable interface. It provides control over the serialization process by allowing classes to implement custom serialization and deserialization logic through the writeExternal() and readExternal() methods.

Q.31 When you serialize an object, what happens to the object references included in the object?

The serialization mechanism generates an object graph for serialization. Thus it determines whether the included object references are serializable or not. This is a recursive process. Thus when an object is serialized, all the included objects are also serialized alongwith the original obect.

Q.32 What happens to the static fields of a class during serialization?

During serialization in Java, static fields are not serialized. They are not part of the state of an object, as they belong to the class itself rather than to individual instances.

Q.33 What are wrapper classes?

Wrapper classes in Java are classes that encapsulate primitive data types, allowing them to be treated as objects. They provide utility methods and are used for conversion between primitive types and objects.

Q.34 Why do we need wrapper classes?

rapper classes are needed in Java for several reasons:

  1. To work with collections: Collections in Java (like ArrayList and HashMap) can only store objects, so wrapper classes allow primitive types to be stored in collections.

  2. To provide utility methods: Wrapper classes provide methods for converting between primitive types and objects, parsing strings, and performing other operations.

  3. To facilitate nullability: Wrapper classes allow null values to be assigned to variables representing primitive types.

  4. To work with generics: Generics in Java do not support primitive types, so wrapper classes are used instead.

Q.35 What are checked exceptions?

Checked exception are those which the Java compiler forces you to catch.

Q.36 What are runtime exceptions?

Runtime exceptions are those exceptions that are thrown at runtime because of either wrong input data or because of wrong business logic etc. These are not checked by the compiler at compile time.

Q.37 What is the difference between error and an exception?

In Java, errors and exceptions are both types of Throwable, but they serve different purposes:

  1. Errors: Errors represent serious, unrecoverable problems that are typically caused by the environment in which the Java program is running. Examples include OutOfMemoryError and StackOverflowError.

  2. Exceptions: Exceptions represent conditions that can be handled and recovered from within the program. They are often caused by programmatic errors or unexpected conditions. Examples include NullPointerException and FileNotFoundException.

Q.38 How to create custom exceptions?

To create a custom exception in Java, follow these steps:

  1. Create a new class that extends either Exception or one of its subclasses (like RuntimeException).
  2. Implement constructors to initialize the exception with a message or cause.
  3. Optionally, add any additional methods or fields as needed.

Here's an example:



    public class CustomException extends Exception {
        public CustomException() {
            super();
        }
    
        public CustomException(String message) {
            super(message);
        }
    
        public CustomException(String message, Throwable cause) {
            super(message, cause);
        }
    }

ou can then throw this custom exception in your code using the throw keyword.

Q.39 If I write return at the end of the try block, will the finally block still execute?

Yes, the finally block will still execute even if a return statement is encountered inside the try block.

Q.40 How are Observer and Observable used?

In Java, the Observer pattern is implemented using the Observer interface and the Observable class.

  1. Observable: This class represents the subject being observed. It maintains a list of observers and notifies them of any changes using the notifyObservers() method.

  2. Observer: This interface represents the observer that wants to be notified of changes in the observable. It defines the update() method, which is called by the observable when it changes.

To use them:

  1. Extend Observable and implement the logic that changes the state of the object.
  2. Implement Observer in the classes that need to be notified of changes.
  3. Register observers with the observable using the addObserver() method.
  4. When the state changes, call setChanged() followed by notifyObservers() to notify all registered observers.



Advertisement





it pathshaala : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML 5

Python

java

C++

C

JavaScript

Campus Learning

C

C#

java